package com.group.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.util.StringUtils;
import pojo.entity.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    /**
     * 登陆界面
     */
    @GetMapping("/")
    public String login() {
        return "login.html";
    }

    /**
     * 聊天界面
     */
    @GetMapping("/index")
    public String index(String username, String password, HttpServletRequest request, HttpServletResponse response) throws UnknownHostException {
        HttpSession httpSession=request.getSession();
        System.out.println(httpSession);
        User user = (User) httpSession.getAttribute("user");
        if (user!=null){
            username=user.getUserName();
            System.out.println(username);
        }else {
            username = "匿名用户";
        }
        /*if (StringUtils.isEmpty(username)) {
            username = "匿名用户";
        }*/
//        ModelAndView mav = new ModelAndView("/chat");
        request.setAttribute("username",username);
        request.setAttribute("webSocketUrl","ws://"+ InetAddress.getLocalHost().getHostAddress()+":"+request.getServerPort()+request.getContextPath()+"/chat");
//        mav.addObject("username", username);
//        mav.addObject("webSocketUrl", "ws://"+ InetAddress.getLocalHost().getHostAddress()+":"+request.getServerPort()+request.getContextPath()+"/chat");
        return "chat.html";
//        return "mav";
    }

}
